package com.itheima.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.excption.MyException;
import com.itheima.pojo.UmsMenu;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import com.itheima.pojo.UmsResource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UmsResourceService {
    @DubboReference
    private UmsResourceApi umsResourceApi;

    /**
     * 添加后台资源
     *
     * @param umsResource
     */
    public void create(UmsResource umsResource) {
        Boolean flag = umsResourceApi.save(umsResource);
        if (!flag) {
            //名称重复
            throw new MyException("秒杀活动已存在,添加失败");
        }

    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public void delete(Integer id) {
        umsResourceApi.delete(id);
    }


    /**
     * 修改
     * @param id
     * @param umsResource
     */
    public void update(Integer id, UmsResource umsResource) {
        umsResourceApi.update(id, umsResource);
    }

    /**
     * 根据id获取详情
     * @param id
     * @return
     */
    public UmsResource findId(Integer id) {
        UmsResource umsResource = umsResourceApi.findId(id);
        return umsResource;
    }

    /**
     * 查询所有信息
     * @return
     */
    public List<UmsResource> findAll() {
            List<UmsResource> result = umsResourceApi.findAll();
            return result;
        }

    /**
     * 分页模糊查询
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @return
     */
    public PageResultVo<UmsResource> list(Integer pageNum, Integer pageSize, Integer categoryId, String nameKeyword, String urlKeyword) {
        IPage<UmsResource> iPage= umsResourceApi.list(pageNum,pageSize,categoryId,nameKeyword,urlKeyword);
        PageResultVo resultVo=new PageResultVo();
        resultVo.setPageNum(pageNum);
        resultVo.setPageSize(pageSize);
        resultVo.setList(iPage.getRecords());
        resultVo.setTotal(Convert.toInt(iPage.getTotal()));
        resultVo.setTotalPages(Convert.toInt(iPage.getPages()));
        return resultVo;
    }
}
